﻿using Dapper;
using HR.IServices.PengIService;
using HR.Models.PengModels;
using System.Data;
using System.Data.SqlClient;
using static System.Net.Mime.MediaTypeNames;

namespace HR.Services.PengService
{
    public class Three_kindServices : IThree_kindServices
    {
        private readonly IConfiguration conf;
        private readonly string? conStr;

        public Three_kindServices(IConfiguration conf)
        {
            this.conf = conf;
            conStr = this.conf["ConnectionStrings"];
        }
        public Task<int> Add(Three_kind third_Kind)
        {
            return Task.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(conStr))
                {
                    return con.Execute($" insert into config_file_third_kind(first_kind_id, first_kind_name, second_kind_id, second_kind_name, third_kind_id, third_kind_name, third_kind_sale_id, third_kind_is_retail) values('{third_Kind.First_kind_id}','{third_Kind.First_kind_name}','{third_Kind.Second_kind_id}','{third_Kind.Second_kind_name}','{third_Kind.Third_kind_id}','{third_Kind.Third_kind_name}','{third_Kind.Third_kind_sale_id}','{third_Kind.Third_kind_is_retail}')");
                }
            });
        }

        public Task<int> Delete(int ftk_id)
        {
            return Task.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(conStr))
                {
                    return con.Execute($" delete from config_file_third_kind where ftk_id = '{ftk_id}'");
                }
            });
        }

        public Task<List<Three_kind>> FindAll()
        {
            return Task.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(conStr)) 
                {
                    return con.Query<Three_kind>($"select * from config_file_third_kind").ToList();
                }
            });
        }

        public Task<FenYe<Three_kind>> ThreeFenYe(int size, int currentPage)
        {
            return Task.Run(() =>
            {
                using (SqlConnection conn = new SqlConnection(conStr))
                {
                    DynamicParameters dynamicParameters = new DynamicParameters();
                    dynamicParameters.Add("@pageSize", size);
                    dynamicParameters.Add("@keyName", "ftk_id");
                    dynamicParameters.Add("@tabelName","config_file_third_kind");
                    dynamicParameters.Add("@where", "");
                    dynamicParameters.Add("@currentPage", currentPage);
                    dynamicParameters.Add("@lie", "*");
                    dynamicParameters.Add("@rows", dbType: DbType.Int32, direction: ParameterDirection.Output);
                    string sql = "exec proc_Fenye @pageSize, @keyName, @tabelName, @where, @currentPage, @lie, @rows out";
                    List<Three_kind> list = conn.Query<Three_kind>(sql, dynamicParameters).ToList();
                    int rows = dynamicParameters.Get<int>("@rows");
                    FenYe<Three_kind> fenYe = new FenYe<Three_kind>()
                    {
                        Rows = rows,
                        Three = list
                    };
                    return fenYe;
                }
            });
        }

        public Task<List<Two_kind>> TwoName()
        {
            return Task.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(conStr))
                {
                    return con.Query<Two_kind>($"select second_kind_id,second_kind_name from config_file_second_kind").ToList();
                }
            });
        }

        public Task<int> Update(Three_kind third_Kind)
        {
            return Task.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(conStr))
                {
                    return con.Execute($" update config_file_third_kind set first_kind_id = '{third_Kind.First_kind_id}',first_kind_name = '{third_Kind.First_kind_name}', second_kind_id = '{third_Kind.Second_kind_id}', second_kind_name = '{third_Kind.Second_kind_name}', third_kind_id = '{third_Kind.Third_kind_id}', third_kind_name = '{third_Kind.Third_kind_name}', third_kind_sale_id = '{third_Kind.Third_kind_sale_id}', third_kind_is_retail = '{third_Kind.Third_kind_is_retail}' where ftk_id = '{third_Kind.Ftk_id}'");
                }
            });
        }
    }
}
